package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.SysWarehouseMapper;
import com.hqyj.pojo.SysWarehouse;
import com.hqyj.service.WarehouseService;
import org.junit.runner.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Autowired
    private SysWarehouseMapper dao;
    //进货
    public HashMap<String, Object> addWareHouse(SysWarehouse warehouse) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        String name = dao.selectMaterial(warehouse.getMaterial());
        if (name != null && name != "") {
            map.put("info", "仓库中已存在该食材，只需要进行续货操作");
        } else {
            int i = dao.insertSelective(warehouse);
            if (i > 0) {
                map.put("info", "添加成功");
            }
        }
        return map;
    }

    // 查询食材列表
    public HashMap<String, Object> wareHouseList(SysWarehouse warehouse) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //模糊查询判断
        System.out.println(warehouse);
        if (warehouse.getMaterial() != null && !warehouse.getMaterial().equals("")) {
            warehouse.setMaterial("%" + warehouse.getMaterial() + "%");
        }
        //分页
        PageHelper.startPage(warehouse.getPage(), warehouse.getRow());
        //2 查询
        List<SysWarehouse> list = dao.selectList(warehouse);

        //3 获取分页对象
        PageInfo<SysWarehouse> pageInfo = new PageInfo<SysWarehouse>(list);


        if (list.size() == 0) {
            map.put("info", "没有查询到数据");
        } else {
            //获取当前页的集合
            map.put("info", pageInfo.getList());
            //总条数
            Long total = pageInfo.getTotal();
            //总页数
            int totalPage = pageInfo.getPages();
            System.out.println("上一页=" + pageInfo.getPrePage());
            System.out.println("下一页=" + pageInfo.getNextPage());
            map.put("total", total);
            map.put("totalPage", totalPage);
        }
        return map;
    }

    //续货，更新食材的数量
    public HashMap<String, Object> wareHouseUpdate(SysWarehouse warehouse) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int n = dao.selectNumberBymaterial(warehouse.getMaterial());
        warehouse.setwNumber(n + warehouse.getwNumber());
        int i = dao.updateNumber(warehouse);
        if (i > 0) {
            map.put("info", "续货成功");
        } else {
            map.put("info", "续货失败");
        }
        return map;
    }
//查询食材列表

    public List<SysWarehouse> materialAll() {

        List<SysWarehouse> list = dao.selectAllMaterial();
//        HttpSession session = request.getSession();
//        session.setAttribute("materList",list);
        return list;
    }

    //退货
    public HashMap<String, Object> wareHouseReturn(SysWarehouse warehouse) {

        HashMap<String, Object> map = new HashMap<String, Object>();
        //查询数据库中现有的食材数目
        System.out.println(warehouse);
        int n = dao.selectNumberBymaterial(warehouse.getMaterial());
        System.out.println(n);
        System.out.println(warehouse);
        if (n < warehouse.getwNumber()) {
            map.put("info", "仓库现有食材不足，无法退货");
        } else {
            warehouse.setwNumber(n - warehouse.getwNumber());

            int i = dao.returnNumber(warehouse);
            if (i > 0) {
                map.put("info", "退货成功");
            } else {
                map.put("info", "退货失败");
            }
        }


        return map;
    }



}
